package mo_gorm

import (
	"MoSkeleton/framework/core/config/properties_db"
	"MoSkeleton/framework/core/moerrors"
	"gorm.io/gorm"
)

// Postgresql 数据库
type MoPgsqlDb struct {
}

func (m *MoPgsqlDb) GenDb(dbConfig *properties_db.GeneralDBProvider, testConn bool) (db *gorm.DB, err error) {
	dbProvider := *dbConfig
	if pgsqlConfig, ok := dbProvider.(*properties_db.Pgsql); ok {
		return m.genPgsqlByConfig(pgsqlConfig, testConn)
	}
	return nil, moerrors.NewWithMsg("dbConfig is not pgsql")
}

func (m *MoPgsqlDb) genPgsqlByConfig(dbConfig *properties_db.Pgsql, testConn bool) (*gorm.DB, error) {
	//if dbConfig.Dbname == "" {
	//	return nil
	//}
	//pgsqlConfig := postgres.Config{
	//	DSN:                       dbConfig.Dsn(), // DSN data source name
	//	DefaultStringSize:         191,            // string 类型字段的默认长度
	//	SkipInitializeWithVersion: false,          // 根据版本自动配置
	//}
	//if db, err := gorm.Open(postgres.New(pgsqlConfig), gormConfig(dbConfig.GeneralDB, dbConfig.Prefix, dbConfig.Singular)); err != nil {
	//	panic(err)
	//} else {
	//	sqlDB, _ := db.DB()
	//	sqlDB.SetMaxIdleConns(dbConfig.MaxIdleConns)
	//	sqlDB.SetMaxOpenConns(dbConfig.MaxOpenConns)
	//	return db
	//}
	panic("uncomment up codes")
}
